package com.youhong.teethcare;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.youhong.teethcare.services.Common;
import com.youhong.teethcare.utils.AppManager;

public class ModeIntroduceActivity extends BaseActivity {
    TextView tv;
    ImageView iv_selected_clean, iv_selected_white,
            iv_selected_polish, iv_selected_soft, iv_selected_sensitive;
    Button btn_next;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mode_introduce);
        initStatusBar(getResources().getColor(R.color.pageBackground), true);
        if (AppManager.getInstance().getSpecificActivity(MainActivity.class) == null) {
            AppManager.getInstance().addActivity(this);
        }

        tv = findViewById(R.id.modeIntroduce_tv);
        iv_selected_clean = findViewById(R.id.modeIntroduct_iv_clean);
        iv_selected_white = findViewById(R.id.modeIntroduct_iv_white);
        iv_selected_polish = findViewById(R.id.modeIntroduct_iv_polish);
        iv_selected_soft = findViewById(R.id.modeIntroduct_iv_soft);
        iv_selected_sensitive = findViewById(R.id.modeIntroduct_iv_sensitive);
        btn_next = findViewById(R.id.modeIntroduce_btn_next);
        btn_next.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (AppManager.getInstance().getSpecificActivity(MainActivity.class) != null) {

                    //同时关闭前一个页面，退回设置页面
                    AppManager.getInstance().killTopActivity();
                    ModeIntroduceActivity.this.finish();

                } else {
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent);

                }
            }
        });
        int mode = initializeVariables();

        if (MainActivity.mDevice != null) {

            MainActivity.mDevice.setGear((byte) mode);

        }
    }

    private int initializeVariables() {

        int teethCondition = Common.userInfo.getCondition();
        int myHabit = Common.userInfo.getManner();
        int resourceId = 0;


       /* "个人信息界面名字（默认Teethcare），生日（默认1st,1,1990,格式为日月年），性别（默认男Male），左右手（默认右手Right），平时习惯如喝咖啡，抽烟等（可多选），牙齿类型（可多选），牙齿美白度，按夏磊设计的UI即可，不必与这个完全相同 – 这里要夏磊加一个Others （其他的选项）
        个人信息填写完成，弹出刷牙模式提醒
        Coffee+正常: According to your teeth condition and habit, Clean mode is recommend to use for whole mouth clean, which can effectively clean every surface, even between the teeth and along the gumline, for a difference you can see and feel（Clean Mode 推荐）
        Coffee+敏感: According to your teeth condition and habit, Sensitive mode recommend for an extra-gentle mode for sensitive teeth, suitable most adults suffer from gum disease at some point in their lives.（Sensitive mode推荐）
        Coffee+牙龈出血:According to your teeth condition and habit，Soft mode recommend for gently stimulates and massages gum, help stimulate the blood circulation around the gums(Soft mode推荐)
        Tea+正常:Clean mode（同上）
        Tea+敏感:Sensitive mode （同上）
        Tea+牙龈出血:Soft mode （同上）

        Red Wine+正常:Clean mode（同上）
        Red Wine+敏感:Sensitive mode（同上）
        Red Wine+牙龈出血:Soft mode（同上）
        Smoking+正常:According to your teeth condition and habit, White mode recommend to remove coffee, tea, tobacco and red wine stains（White mode推荐）

        Smoking+敏感:Sensitive mode（同上）

        Smoking+牙龈出血:Soft mode（同上）
        Other+：则推荐Clean模式(后续添加)"
*/
        //iv_selected_white.setVisibility(View.VISIBLE);
        //iv_selected_clean.setVisibility(View.VISIBLE);
        //iv_selected_sensitive.setVisibility(View.VISIBLE);
        //iv_selected_soft.setVisibility(View.VISIBLE);
        //iv_selected_white.setVisibility(View.VISIBLE);

        //bitmap: bit0:喝咖啡,bit1:喝茶,bit2:喝红酒,bit3s:抽烟
        //0:健康,1:敏感,2:牙龈出血,3:牙齿发黄
        int mode = 0;
        switch (teethCondition) {

            case 0:
                if ((myHabit & 0x1) != 0 || (myHabit & 0x2) != 0 || (myHabit & 0x4) != 0) {
                    resourceId = R.string.mode_introduce_clean;
                    iv_selected_clean.setVisibility(View.VISIBLE);
                    mode = 1;
                } else if ((myHabit & 0x8) != 0) {

                    resourceId = R.string.mode_introduce_white;
                    iv_selected_white.setVisibility(View.VISIBLE);
                    mode = 2;
                } else {

                    resourceId = R.string.mode_introduce_clean;
                    iv_selected_clean.setVisibility(View.VISIBLE);
                    mode = 1;
                }

                break;
            case 1:
                if ((myHabit & 0x1) != 0 || (myHabit & 0x2) != 0 || (myHabit & 0x4) != 0
                        || (myHabit & 0x8) != 0) {
                    resourceId = R.string.mode_introduce_sensitive;
                    iv_selected_sensitive.setVisibility(View.VISIBLE);
                    mode = 5;
                } else {

                    resourceId = R.string.mode_introduce_sensitive;
                    iv_selected_sensitive.setVisibility(View.VISIBLE);
                    mode = 5;
                }
                break;
            case 2: {
                resourceId = R.string.mode_introduce_soft;
                iv_selected_soft.setVisibility(View.VISIBLE);
                mode = 4;
            }
                break;
            case 3:
                resourceId = R.string.mode_introduce_white;
                iv_selected_white.setVisibility(View.VISIBLE);
                mode = 2;
                break;
            default:

                resourceId = R.string.mode_introduce_white;
                iv_selected_white.setVisibility(View.VISIBLE);
                mode = 2;
                break;
        }

        //默认设置Clean Mode
        if (resourceId == 0) {
            resourceId = R.string.mode_introduce_clean;
            iv_selected_clean.setVisibility(View.VISIBLE);
            mode = 1;
        }

        String promptText = getResources().getString(resourceId);
        tv.setText(Html.fromHtml(promptText));
        return mode;
    }
}
